package org.i1510.common.interceptor;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.i1510.common.bean.SessionUserBean;
import org.i1510.common.utils.KBProperties;
import org.i1510.common.utils.UserUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

public class SessionOutInterceptor extends BaseInterceptor {
	private static Logger logger = LoggerFactory.getLogger(SessionOutInterceptor.class);
	
	

	@Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
//        HandlerMethod handlerM =(HandlerMethod) handler;
        
        if(isAllow(request)){
        	return true;//不拦截直接通过
        } 
          

//		HttpSession session = request.getSession();
		
		SessionUserBean userSession = UserUtils.getSessionUserBean(request);
		if (null == userSession) { //未登录
            if (request.getHeader("x-requested-with") != null && request.getHeader("x-requested-with").equalsIgnoreCase("XMLHttpRequest")){ //如果是ajax请求响应头会有，x-requested-with  
                response.setHeader("SessionTimeout", "true");//在响应头设置session状态 
                response.setHeader("System", "By bobolnear@163.com");
            }else{
            	response.sendRedirect(KBProperties.TIMEOUT_URL);
            }  
            return false;
        }
        return true;
    }

}